using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using CodePlex.Resourceful.Microsoft.Azure;
using System.Net;

namespace CodePlex.Resourceful.Test
{
    [TestClass]
    public class AzureTests
    {


        [TestMethod]
        public void Azure_Tests_Production()
        {
            string[] acct = Environment.GetEnvironmentVariable("AZURESTORAGE").Split(':');

            string accountName = acct[0];
            string primaryAccessKey = acct[1];
            string secondaryAccessKey = acct[2];

            AzureBlobStorageClient client = new AzureBlobStorageClient(accountName, primaryAccessKey);

            Azure_Tests_Common(client);
        }

        [TestMethod]
        public void Azure_Tests_Dev()
        {
            string accountName = AzureConstants.DevelopmentStorageAccountName;
            string primaryAccessKey = AzureConstants.DevelopmentStorageAccountKey;
            string serviceHost = AzureConstants.DevelopmentStorageServiceHost;

            AzureBlobStorageClient client = new AzureBlobStorageClient(accountName, primaryAccessKey, serviceHost, true, false);

            Azure_Tests_Common(client);
        }


        private void Azure_Tests_Common(AzureBlobStorageClient client)
        {

            AzureBlobStorageConnection cn = new AzureBlobStorageConnection(client);

          
            // cleanup
            Cleanup(cn);
            try
            {
                string containerName = "testcontainer" + DateTime.Now.Ticks;

                cn.Tracer.AddListener(Console.Out);
           

                // start
                Assert.IsTrue(!cn.IsContainerPresent(containerName));
                Assert.AreEqual(0, Fn.Count(cn.ListContainers().Containers));

                NameValuePairs metadata = new NameValuePairs();
                metadata.Add("foo", "bar");
                cn.CreateContainer(containerName, metadata);
                Assert.IsTrue(cn.IsContainerPresent(containerName));
                Assert.AreEqual(1, Fn.Count(cn.ListContainers().Containers));

                foreach (ContainerInfo c in cn.ListContainers().Containers)
                {
                    Assert.AreEqual(containerName, c.Name);
                }

                //cn.PutBlob(containerName, "x .x", "");

                metadata = cn.GetContainerProperties(containerName);
                Assert.AreEqual(1, metadata.Count);
                Assert.AreEqual("bar", metadata["foo"]);

                metadata = new NameValuePairs();
                metadata.Add("foo2", "bar2");
                cn.SetContainerMetadata(containerName, metadata);
                metadata = cn.GetContainerProperties(containerName);
                Assert.AreEqual(1, metadata.Count);
                Assert.AreEqual("bar2", metadata["foo2"]);

       
                Assert.AreEqual(false, cn.GetContainerACL(containerName));

                cn.SetContainerACL(containerName, true);
                Assert.AreEqual(true, cn.GetContainerACL(containerName));



                Assert.AreEqual(0, Fn.Count(cn.ListBlobs(containerName).Blobs));
                Assert.AreEqual(cn.GetContainerUri(containerName), cn.ListBlobs(containerName).ContainerUri);

                string blobName = "blob1";
                string blobContents = "asdf";
                cn.PutBlob(containerName, blobName, blobContents);
                Assert.AreEqual(1, Fn.Count(cn.ListBlobs(containerName).Blobs));
                Assert.AreEqual(blobContents, new WebClient().DownloadString(cn.GetBlobUri(containerName, blobName)));

                metadata = cn.GetBlobProperties(containerName, blobName);
                Assert.AreEqual(MediaType.TextPlain, metadata[HttpHeader.ContentType]);
                Assert.AreEqual(blobContents.Length.ToString(), metadata[HttpHeader.ContentLength]);

                blobContents = "asdf2";
                cn.PutBlob(containerName, blobName, blobContents);
                Assert.AreEqual(blobContents, cn.GetBlobAsString(containerName, blobName));

                Assert.AreEqual(0, cn.GetBlobMetadata(containerName, blobName).Count);
                metadata = new NameValuePairs();
                metadata["foo"] = "bar";
                cn.SetBlobMetadata(containerName, blobName,metadata);
                metadata = cn.GetBlobMetadata(containerName, blobName);
                Assert.AreEqual(1, metadata.Count);
                Assert.AreEqual("bar", metadata["foo"]);



                cn.PutBlob(containerName, "blob2", "blob2contents");
                Assert.AreEqual(2, Fn.Count(cn.ListBlobs(containerName).Blobs));
                Assert.IsNull(cn.ListBlobs(containerName).MaxResults);
                Assert.IsNull(cn.ListBlobs(containerName).NextMarker);
                Assert.AreEqual(1, Fn.Count(cn.ListBlobs(containerName, null, null, null, 1).Blobs));
                Assert.AreEqual(1, cn.ListBlobs(containerName, null, null, null, 1).MaxResults.Value);
                Assert.AreEqual("blob2", cn.ListBlobs(containerName, null, null, null, 1).NextMarker);


                cn.DeleteBlob(containerName, blobName);
                Assert.AreEqual(1, Fn.Count(cn.ListBlobs(containerName).Blobs));
                

                cn.DeleteContainer(containerName);
            }
            finally
            {
                cn.Tracer.Listeners.Clear();
                //cn.ClientTracer.Listeners.Clear();

                Cleanup(cn);
            }



        }

        private void Cleanup(AzureBlobStorageConnection cn)
        {
            foreach (ContainerInfo c in cn.ListContainers().Containers)
            {
                cn.DeleteContainer(c.Name);
            }
        }

    }
}
